<template>
  <el-form ref="loginFormRef" class="login-form" :model="userInfo" :rules="rules">
    <div class="login-title">登录</div>
    <lang></lang>
    <el-form-item prop="username">
      <el-input v-model="userInfo.username" placeholder="用户名" :prefix-icon="User" autocomplete="off"
                clearable></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="userInfo.password" placeholder="密码" :prefix-icon="Lock" autocomplete="off" show-password
                type="password"
                @keyup.enter="submitForm" clearable></el-input>
    </el-form-item>
    <div class="checked">
      <div>
        <el-checkbox v-model="checked" label="记住我"/>
      </div>
      <div>
        <span style="font-size: 14px;line-height: 32px">忘记密码？</span>
      </div>
    </div>
    <div class="login-btn">
      <el-button type="primary" @click="submitForm">{{ $t('message.test') }}</el-button>
    </div>
    <div class="login-tab">
      <el-button type="primary" size="small" @click="usePhoneLogin">手机登录</el-button>
      <el-button type="primary" size="small" @click="useQrCodeLogin">二维码登录</el-button>
      <el-button type="primary" size="small" @click="registe">注册</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import {useRouter} from 'vue-router'
import {ElMessage} from "element-plus";
import lang from "../../../src/components/layout/components/header/lang.vue"
import {User, Lock} from '@element-plus/icons-vue'
import {ref, reactive} from "vue";
import {login} from "@/aixos/api";
import {userStore} from "@/store/module/user";

const router = useRouter()
const store = userStore()
const loginFormRef = ref()
const checked = ref(true)

const userInfo = reactive({
  username: '',
  password: ''
})
if (checked.value&&sessionStorage.getItem('user')) {
  // userInfo.username = sessionStorage.getItem('user').userInfo.username
  // userInfo.password = sessionStorage.getItem('user').userInfo.password
}

const rules = reactive({
  username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}]
})
const emit = defineEmits(['on-click'])
const usePhoneLogin = () => {
  emit('on-click', 1)
}
const useQrCodeLogin = () => {
  emit('on-click', 2)
}
const registe = () => {
  emit('on-click', 3)
}
const submitForm = async () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      login(userInfo).then(res => {
        if (res.data.code == 200) {
          ElMessage.success('登陆成功！')
        } else {
          ElMessage.success('登陆失败！')
        }
      })
    } else {
      ElMessage.error('请输入用户名和密码')
    }
  })
}
</script>

<style lang="scss" scoped>
:deep(.el-form-item__error) {
  left: 25px !important;
}

.login-form {
  width: 300px;

  .login-title {
    width: 250px;
    text-align: center;
    margin: 10px auto;
  }

  .el-input {
    margin: 0 auto;
    width: 250px;
  }

  .checked {
    display: flex;
    padding-left: 23px;
    padding-right: 20px;
    margin-bottom: 10px;
    justify-content: space-between;
  }

  .login-btn {
    margin: 0 auto;
    text-align: center;
    margin-bottom: 20px;

    .el-button {
      width: 250px;
    }
  }

  .login-tab {
    width: 250px;
    display: flex;
    justify-content: space-between;
    margin: 0 auto;
  }
}
</style>
